<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/h_something/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/h_something/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/h_something/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/h_something/images/logo.svg" color="#222">

<link rel="stylesheet" href="/h_something/css/main.css">


<link rel="stylesheet" href="/h_something/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ddota2.gitee.io","root":"/h_something/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="关于拆包装包这块，以前一直是一知半解，也觉得没有什么需要特别写出来的必要，反正是 JAVA 自带的性质。最近发现里面还是有一些以前不曾在意的机制的，简单说一下。 1234567891011121314151617181920212223242526public static void main(String[] args) &amp;#123;        System.out.println(new">
<meta property="og:type" content="article">
<meta property="og:title" content="浅谈拆包装包">
<meta property="og:url" content="https://ddota2.gitee.io/2021/01/11/%E6%B5%85%E8%B0%88%E6%8B%86%E5%8C%85%E8%A3%85%E5%8C%85/index.html">
<meta property="og:site_name" content="D">
<meta property="og:description" content="关于拆包装包这块，以前一直是一知半解，也觉得没有什么需要特别写出来的必要，反正是 JAVA 自带的性质。最近发现里面还是有一些以前不曾在意的机制的，简单说一下。 1234567891011121314151617181920212223242526public static void main(String[] args) &amp;#123;        System.out.println(new">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-11T07:03:15.000Z">
<meta property="article:modified_time" content="2021-01-11T08:54:23.345Z">
<meta property="article:author" content="Dota2 player">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://ddota2.gitee.io/2021/01/11/%E6%B5%85%E8%B0%88%E6%8B%86%E5%8C%85%E8%A3%85%E5%8C%85/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>浅谈拆包装包 | D</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/h_something/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">D</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">而已</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/h_something/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/h_something/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/h_something/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/h_something/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2021/01/11/%E6%B5%85%E8%B0%88%E6%8B%86%E5%8C%85%E8%A3%85%E5%8C%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浅谈拆包装包
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-01-11 15:03:15 / 修改时间：16:54:23" itemprop="dateCreated datePublished" datetime="2021-01-11T15:03:15+08:00">2021-01-11</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>关于拆包装包这块，以前一直是一知半解，也觉得没有什么需要特别写出来的必要，反正是 JAVA 自带的性质。<br>最近发现里面还是有一些以前不曾在意的机制的，简单说一下。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="keyword">new</span> Integer(<span class="number">1</span>)); <span class="comment">//1</span></span><br><span class="line">    </span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="number">1</span>); <span class="comment">//2</span></span><br><span class="line">    </span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) + <span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="number">2</span>); <span class="comment">//3</span></span><br><span class="line">    </span><br><span class="line">        Integer a = <span class="number">1</span>;</span><br><span class="line">        Integer b = <span class="number">1</span>;</span><br><span class="line">        System.out.println(a==b); <span class="comment">//4</span></span><br><span class="line">    </span><br><span class="line">        Integer a1 = <span class="number">128</span>;</span><br><span class="line">        Integer b1 = <span class="number">128</span>;</span><br><span class="line">        System.out.println(a1==b1); <span class="comment">//5</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">int</span> a2 = <span class="keyword">new</span> Integer(<span class="number">1236</span>);</span><br><span class="line">        Integer b2 = <span class="keyword">new</span> Integer(<span class="number">1236</span>);</span><br><span class="line">        System.out.println(a2==b2); <span class="comment">//6</span></span><br><span class="line">   </span><br><span class="line">&#125;</span><br><span class="line">可以得到结果:</span><br><span class="line"><span class="keyword">false</span> <span class="comment">//1</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//2</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//3</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//4</span></span><br><span class="line"><span class="keyword">false</span> <span class="comment">//5</span></span><br></pre></td></tr></table></figure>

<ol>
<li>因为自动装包机制，a 和 b 实际指向的是对象的内存地址。而两个 new 出来的对象地址一定不相等</li>
<li>因为自动拆包机制，当包装类型与基础类型比较时，会进行拆包，对基础类型的值进行比较，所以相等</li>
<li>当包装类型进行计算时，会进行自动拆包，故比较的是值，而不是地址，为true。</li>
<li>因为基础类型的缓存机制，int类型会缓存[-128,127]的值，int a = 1,实际上是类似于调用了 int a = Integet.valueOf(1)，如果缓存中存在，则会直接返回该对象，并不会new，所以同一个对象的内存引用地址一样，故相等</li>
<li>同上，只是128超出了缓存的范围，因此是new出来的，所以两个对象引用地址不同。</li>
<li>声明时是 int， int a2 = new Integer(1236) 实际上是 int a2 = new Integer(1236).intValue();  且当包装类型与基础类型比较时，会自动拆表比较值。</li>
</ol>
<p>根据自身理解，自动拆包有一下几种情况：</p>
<ol>
<li>当存在算术符号时</li>
<li>当包装类型与基础类型比较时</li>
<li>声明对象时使用基础类型声明时</li>
</ol>
<p>自动包装有以下几种情况：</p>
<ol>
<li>声明对象为包装类型时，包括new，和直接给定值。区别在于，new的时候一定是一个新的对象，而后者通常调用了XXX.valueOf(a)，有可能会从缓存中直接获取。<ul>
<li>int a = new Integer(1);</li>
<li>int a = 1;</li>
</ul>
</li>
</ol>
<p><strong>Tips</strong>：</p>
<p>包装类型缓存范围如下：</p>
<table>
<thead>
<tr>
<th>类型</th>
<th align="left">范围</th>
</tr>
</thead>
<tbody><tr>
<td>Byte,Short,Integer,Long</td>
<td align="left">[-128,127]</td>
</tr>
<tr>
<td>Character</td>
<td align="left">[0,127] 前128个字符</td>
</tr>
<tr>
<td>Boolean</td>
<td align="left">true,false</td>
</tr>
<tr>
<td>Float, Double</td>
<td align="left">无缓存</td>
</tr>
</tbody></table>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/h_something/tags/java/" rel="tag"># java</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/h_something/2020/12/17/docker-es/" rel="prev" title="docker es">
      <i class="fa fa-chevron-left"></i> docker es
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Dota2 player</p>
  <div class="site-description" itemprop="description">楼下的男人病的要死</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/h_something/archives/">
        
          <span class="site-state-item-count">11</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/h_something/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/h_something/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dota2 player</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/h_something/lib/anime.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.ui.min.js"></script>

<script src="/h_something/js/utils.js"></script>

<script src="/h_something/js/motion.js"></script>


<script src="/h_something/js/schemes/muse.js"></script>


<script src="/h_something/js/next-boot.js"></script>




  















  

  

</body>
</html>
